package com.moxiao.yfjh.module.system.controller.admin.memmessage;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.system.controller.admin.memmessage.vo.MemMessageCreateReqVO;
import com.moxiao.yfjh.module.system.controller.admin.memmessage.vo.MemMessageExcelVO;
import com.moxiao.yfjh.module.system.controller.admin.memmessage.vo.MemMessageExportReqVO;
import com.moxiao.yfjh.module.system.controller.admin.memmessage.vo.MemMessagePageReqVO;
import com.moxiao.yfjh.module.system.controller.admin.memmessage.vo.MemMessageRespVO;
import com.moxiao.yfjh.module.system.controller.admin.memmessage.vo.MemMessageUpdateReqVO;
import com.moxiao.yfjh.module.system.convert.memmessage.MemMessageConvert;
import com.moxiao.yfjh.module.system.dal.dataobject.memmessage.MemMessageDO;
import com.moxiao.yfjh.module.system.service.memmessage.MemMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 聊天记录")
@RestController
@RequestMapping("/sys/mem-message")
@Validated
public class MemMessageController {

    @Resource
    private MemMessageService memMessageService;

    @PostMapping("/create")
    @Operation(summary = "创建聊天记录")
    @PreAuthorize("@ss.hasPermission('sys:mem-message:create')")
    public CommonResult<Integer> createMemMessage(@Valid MemMessageCreateReqVO createReqVO) throws UnsupportedEncodingException {
        if (Objects.nonNull(createReqVO.getMessageContent())){
            String decode = URLDecoder.decode(createReqVO.getMessageContent(), "UTF-8");
            createReqVO.setMessageContent(decode);
        }
        return success(memMessageService.createMemMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新聊天记录")
    @PreAuthorize("@ss.hasPermission('sys:mem-message:update')")
    public CommonResult<Boolean> updateMemMessage(@Valid @RequestBody MemMessageUpdateReqVO updateReqVO) {
        memMessageService.updateMemMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除聊天记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:mem-message:delete')")
    public CommonResult<Boolean> deleteMemMessage(@RequestParam("id") Integer id) {
        memMessageService.deleteMemMessage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得聊天记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:mem-message:query')")
    public CommonResult<MemMessageRespVO> getMemMessage(@RequestParam("id") Integer id) {
        MemMessageDO memMessage = memMessageService.getMemMessage(id);
        return success(MemMessageConvert.INSTANCE.convert(memMessage));
    }

    @GetMapping("/list")
    @Operation(summary = "获得聊天记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sys:mem-message:query')")
    public CommonResult<List<MemMessageRespVO>> getMemMessageList(@RequestParam("ids") Collection<Integer> ids) {
        List<MemMessageDO> list = memMessageService.getMemMessageList(ids);
        return success(MemMessageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得聊天记录分页")
    @PreAuthorize("@ss.hasPermission('sys:mem-message:query')")
    public CommonResult<PageResult<MemMessageRespVO>> getMemMessagePage(@Valid MemMessagePageReqVO pageVO) {
        PageResult<MemMessageDO> pageResult = memMessageService.getMemMessagePage(pageVO);
        return success(MemMessageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出聊天记录 Excel")
    @PreAuthorize("@ss.hasPermission('sys:mem-message:export')")
    @OperateLog(type = EXPORT)
    public void exportMemMessageExcel(@Valid MemMessageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemMessageDO> list = memMessageService.getMemMessageList(exportReqVO);
        // 导出 Excel
        List<MemMessageExcelVO> datas = MemMessageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "聊天记录.xls", "数据", MemMessageExcelVO.class, datas);
    }

}
